package com.sp2.dev.apachecommons;

import org.apache.commons.lang.ArrayUtils;
import org.junit.Test;

/**
 * Created by IntelliJ IDEA.
 * User: zhimingli
 * Date: 14-2-17
 * Time: 下午12:02
 * To change this template use File | Settings | File Templates.sss
 */
public class ArrayUtilsTest {
    @Test
    public void testToString() {
        int[] intArray = new int[]{6, 1, 2, 5};
        //intArray: {6,1,2,5}
        System.out.println("intArray toString:" + ArrayUtils.toString(intArray));
    }

    //互相转换
    @Test
    public void toObjectAndToPrimitive() {
        long[] primitiveArray = new long[]{12, 100, 2929, 3323};
        Long[] objectArray = ArrayUtils.toObject(primitiveArray);
        System.out.println(ArrayUtils.toString(objectArray));

        Double[] doubleObjects = new Double[]{new Double(44.222), new Double(6546.456456), new Double(4654.4578)};
        double[] doublePrimitives = ArrayUtils.toPrimitive(doubleObjects);
        System.out.println(ArrayUtils.toString(doublePrimitives));
    }

    //需要在数组中找一个值的时候，很方便使用
    @Test
    public void testArrayContains() {
        String[] stringArray = new String[]{"a", "b", "c"};
        boolean ok1 = ArrayUtils.contains(stringArray, "a");
        boolean ok2 = ArrayUtils.contains(stringArray, "A");


        System.out.println(ok1 + "---" + ok2);


        int indexOfRed = ArrayUtils.indexOf( stringArray, "a");
        System.out.println(indexOfRed+"---"+stringArray[0]);

        Usercommon[] usercommons = new Usercommon[]{new Usercommon("a",1),new Usercommon("b",2)};
        boolean userOK  = ArrayUtils.contains(usercommons, new Usercommon("a",1));
        System.out.println(userOK);
    }
}
